package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.emtity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;



    /*
        添加到购物车
        添加套餐到购物车
        POST http://localhost:8080/shoppingCart/add
           {"amount":50,"setmealId":"1586616156652236802","name":"架构师套餐","image":"2ba69fb6b4f14b4fac6146b9209670a1.png"}

        添加某道菜到购物车
         post http://localhost:8080/shoppingCart/add
         {"amount":20,"dishFlavor":"微辣","dishId":"1397854865672679425","name":"鱼香炒鸡蛋","image":"0f252364-a561-4e8d-8065-9a6797a6b1d3.jpg"}

       select *  from shopping_cart where user_id='xxxx' and dish_id='disID'
       select *  from shopping_cart where user_id='xxxx' and setmeal_id='setmealid'


    */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        //获取当前用户的id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //添加菜品或者套餐
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        //判断添加的是菜品还是套餐
        if(shoppingCart.getDishId() != null){
            //查询购物车数据是否存在
            //select *  from shopping_cart where user_id='xxxx' and dish_id='disID'
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            //shoppingCartService.save(shoppingCart);
        }
        ShoppingCart sone = shoppingCartService.getOne(lqw);
        //判断sone是否为空
        if(sone != null){
            //已经有数据菜品或者套餐了，套餐数量+1
            sone.setNumber(sone.getNumber()+1);
            shoppingCartService.updateById(sone);
        }else {
            //第一次买
            sone = shoppingCart;
            sone.setNumber(1);
            sone.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(sone);
        }
        return R.success("添加成功");
    }


    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }


    //*_清空购物车
    //DELETE  http://localhost:8080/shoppingCart/clean
    //DELETE  FROM shopping_cart  WHERE user_id=''
    @DeleteMapping("/clean")
    public R<String> delete(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(lqw);
        return R.success("清空成功");
    }

 }  